<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="com.liferay.portal.kernel.dao.search.RowChecker"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiXuLyHoSoXoaAnTichEnum"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiTienSuEnum"%>
<%@page import="qldc.pm.tiensu.service.PhieuThongTinVeNguoiCoTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.PhieuThongTinVeNguoiCoTienSu"%>
<%@page import="qldc.pm.tiensu.service.TienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.TienSu"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.model.DoiTuongTienSu"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@page import="qldc.pm.tiensu.searchcontainer.YeuCauXoaAnTichSearchTerms"%>
<%@page import="qldc.pm.tiensu.searchcontainer.YeuCauXoaAnTichDisplayTerms"%>
<%@page import="qldc.pm.tiensu.searchcontainer.YeuCauXoaAnTichSearch"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="java.util.List"%>
<%@include file="../../init.jsp"%>

<%
PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("mvcPath", "/html/view.jsp");
portletURL.setParameter("chonChucNangItem", TienSuConstants.QUAN_LY_YEU_CAU_XOA_AN_TICH);

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String pathThemeImages = themeDisplay.getPathThemeImages();

String chonHoSoXoa = ParamUtil.getString(request, "chonHoSoXoa");

%>

<portlet:actionURL var="kyDuyetDanhSachURL">
	<portlet:param name="<%=TienSuConstants.ACTION_HANDLER_PARAM %>" value="<%=TienSuConstants.QUAN_LY_NGUOI_CO_TIEN_SU %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="kyDuyetDanhSachXoaAnTich"/>
	<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
</portlet:actionURL>

<script type="text/javascript">
$(function() {
	$("#<portlet:namespace/>ngayLapHoSoXoaAnTich").datepicker({
		showOn: "both",
		buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich" />",
			dateFormat : "dd/mm/yy"
			
		});
	

});

function kyDuyetdanhSach() {
	var co = '<liferay-ui:message key="co" />';
 	var khong = '<liferay-ui:message key="khong" />';
 	var mess = '<liferay-ui:message key="ban-co-muon-luu-thong-tin" />';
		showPopupConfirm(mess,[co, khong], function(index) {
		    if (index == 0){
		    	<%
		    	portletURL.setParameter("chonHoSoXoa", TienSuConstants.KY_DUYET_HO_SO_XOA);

		    	%>
		    	var rowIds = '<portlet:namespace/>allRowIds';
		    	var selectList = getCheckBoxSelectedOnSearchContainer('<portlet:namespace/>xoaAnTichSearchContainer',rowIds);
		    	$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.KY_DUYET_HO_SO_XOA%>');
		    	$('#<portlet:namespace/>lichSuXoaAnTichIds').val(selectList);
		    	$("#<portlet:namespace/>fm").attr('action', '<%=kyDuyetDanhSachURL.toString() %>');
		    	$("#<portlet:namespace/>fm").submit(); 
			} 
		});
 	
}
	
function chonHoSoXoaDeKyDuyet() {
	<%
	portletURL.setParameter("chonHoSoXoa", TienSuConstants.CHON_HO_SO_XOA);

	%>
	
	$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.CHON_HO_SO_XOA%>');
	$("#<portlet:namespace/>fm").attr('action', '<%=portletURL.toString()%>');
	$("#<portlet:namespace/>fm").submit(); 
}

function timKiem() {
	<%
	if (chonHoSoXoa.equals(TienSuConstants.CHON_HO_SO_XOA)){
		portletURL.setParameter("chonHoSoXoa", TienSuConstants.CHON_HO_SO_XOA);
	}else {
		portletURL.setParameter("chonHoSoXoa", TienSuConstants.TIM);
	}
	%>
	$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.TIM%>');

	$("#<portlet:namespace/>fm").attr('action', '<%=portletURL.toString()%>');
	$("#<portlet:namespace/>fm").submit(); 
}

function closeKyDuyet() {
	<%
		portletURL.setParameter("chonHoSoXoa", TienSuConstants.DONG);
	
	%>
	$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.DONG%>');

	$("#<portlet:namespace/>fm").attr('action', '<%=portletURL.toString()%>');
	$("#<portlet:namespace/>fm").submit(); 
}
</script>

<form action="<%=portletURL.toString() %>" method="POST" id="<portlet:namespace />fm" name="<portlet:namespace />fm">
	<input type="hidden" name="<portlet:namespace />cmd" id="<portlet:namespace />cmd">
	<input type="hidden" name="<portlet:namespace/>lichSuXoaAnTichIds" id="<portlet:namespace/>lichSuXoaAnTichIds">

	<%
	
	RowChecker rowChecker = new RowChecker(renderResponse);
	if(!chonHoSoXoa.equals(TienSuConstants.CHON_HO_SO_XOA)) {
		rowChecker = null;
	}
	
	YeuCauXoaAnTichSearch yeuCauXoaAnTichSearch = new YeuCauXoaAnTichSearch(renderRequest, portletURL);
	YeuCauXoaAnTichDisplayTerms displayTerms = (YeuCauXoaAnTichDisplayTerms) yeuCauXoaAnTichSearch.getDisplayTerms();
	YeuCauXoaAnTichSearchTerms searchTerms = (YeuCauXoaAnTichSearchTerms) yeuCauXoaAnTichSearch.getSearchTerms();
	int currentStatus = searchTerms.getTrangThaiXuLyHoSoXoaAnTich();
	
	if (currentStatus == TrangThaiXuLyHoSoXoaAnTichEnum.TAT_CA.getValue()){
		currentStatus = TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET.getValue();
	}
	
	int totalRow = LichSuXoaAnTichLocalServiceUtil.countByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(searchTerms.getHoVaTenNguoiYeuCauXoaAnTich(), searchTerms.getMaSoHoSoXoaAnTich(), searchTerms.getNgayLapHoSoXoaAnTich(),currentStatus);
	%>
	
	<table class="global-Search">

		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="ho-va-ten" /></td>
			<td class="aui-w30"><input type="text" name="<portlet:namespace/><%=YeuCauXoaAnTichDisplayTerms.HO_VA_TEN_NGUOI_YEU_CAU_XOA_AN_TICH %>" value="<%=searchTerms.getHoVaTenNguoiYeuCauXoaAnTich() %>" /></td>
			<td class="txtlabel aui-w20"><liferay-ui:message key="trang-thai" /></td>
			<td class="aui-w30">
			<%
				TrangThaiXuLyHoSoXoaAnTichEnum [] trangThais = TrangThaiXuLyHoSoXoaAnTichEnum.values(); 

			%>
				<select id="<portlet:namespace/>trangThaiXuLyHoSoXoaAnTich" name="<portlet:namespace/><%=YeuCauXoaAnTichDisplayTerms.TRANG_THAI_XU_LY_HO_SO_XOA_AN_TICH %>" >
					<%
					for(int i = 0; i < trangThais.length; i++) {
						if (trangThais[i].getValue() != TrangThaiXuLyHoSoXoaAnTichEnum.TAT_CA.getValue()){
					%>
						<option value=<%=trangThais[i].getValue() %> <%= (trangThais[i].getValue() == currentStatus || (trangThais[i].getValue() == TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET.getValue() && currentStatus == TrangThaiXuLyHoSoXoaAnTichEnum.TAT_CA.getValue())) ? "selected = 'true'" : ""%>><liferay-ui:message key="<%=trangThais[i].getKey() %>" /></option>
					<%
						}
					}
					%>
				</select>
			</td>
		</tr>

		<tr>
			<td class="txtlabel aui-w20"><liferay-ui:message key="ma-so-ho-so-xoa-an-tich" /></td>
			<td class="aui-w30">
			<input type="text" name="<portlet:namespace/><%=YeuCauXoaAnTichDisplayTerms.MA_SO_HO_SO_XOA_AN_TICH %>" value="<%=searchTerms.getMaSoHoSoXoaAnTich() %>" /></td>
			<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-lap-ho-so-xoa-an-tich" /></td>
			
			<td class="aui-w30 ip-datepicker"><input type="text" style="width: 90%"  id="<portlet:namespace/>ngayLapHoSoXoaAnTich" name="<portlet:namespace/><%=YeuCauXoaAnTichDisplayTerms.NGAY_LAP_HO_SO_XOA_AN_TICH %>" value="<%=searchTerms.getNgayLapHoSoXoaAnTich() != null ? sdf.format(searchTerms.getNgayLapHoSoXoaAnTich()) : StringPool.BLANK %>" /></td>
			
		</tr>
		
		<tr>
			<td></td>
			<td colspan="3">
				<div align="left">
					<input type="button" onclick="timKiem()" class="btn btn-primary" value="<liferay-ui:message key="tim" />" />
					<% 
					if(chonHoSoXoa.equals(TienSuConstants.CHON_HO_SO_XOA)) {
					%>
					<input type="button" onclick="kyDuyetdanhSach()" class="btn btn-primary" value="<liferay-ui:message key="ky-duyet-danh-sach-xoa-an-tich" />" />
					<input type="button" onclick="closeKyDuyet()" class="btn " value="<liferay-ui:message key="dong" />" />
					<%
					}
					if (totalRow > 0 && currentStatus == TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET.getValue() && !(chonHoSoXoa.equals(TienSuConstants.CHON_HO_SO_XOA))) {
					%>					
					<input type="button" onclick="chonHoSoXoaDeKyDuyet()" class="btn btn-primary" value="<liferay-ui:message key="chon-an-tich-de-xoa" />" />
					<%
					}
					%>
				</div>
			</td>
		</tr>	
	</table>
</form>

<liferay-ui:search-container id="xoaAnTichSearchContainer" rowChecker="<%=rowChecker %>"  searchContainer="<%=yeuCauXoaAnTichSearch %>">
	<liferay-ui:search-container-results>		
		<%
			results = LichSuXoaAnTichLocalServiceUtil
							.findByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(searchTerms.getHoVaTenNguoiYeuCauXoaAnTich(),
									searchTerms.getMaSoHoSoXoaAnTich(),
									searchTerms.getNgayLapHoSoXoaAnTich(),currentStatus
									,yeuCauXoaAnTichSearch.getStart(), yeuCauXoaAnTichSearch.getEnd(), 
									yeuCauXoaAnTichSearch.getOrderByComparator());
				
					total = totalRow;
					pageContext.setAttribute("results", results);
					pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	<%
	int start = yeuCauXoaAnTichSearch.getDelta() * (yeuCauXoaAnTichSearch.getCur() - 1);
	%>
	<liferay-ui:search-container-row className="qldc.pm.tiensu.model.LichSuXoaAnTich" keyProperty="lichSuXoaAnTichId" modelVar="lichSuXoaAnTich" >
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name='STT' value="<%=String.valueOf(++start) %>" />
		
		<%
		String diaChi = "";
		String banAnSo = "";
		String trangThai = "";
		TienSu ts = TienSuLocalServiceUtil.findByTienSuId(lichSuXoaAnTich.getTienSuId());
		if (Validator.isNotNull(ts)){
			banAnSo = ts.getBanAnSo();
			PhieuThongTinVeNguoiCoTienSu phieu = PhieuThongTinVeNguoiCoTienSuLocalServiceUtil.findByTienSuId(ts.getTienSuId());
			if (Validator.isNotNull(phieu)){
				diaChi = DiaChiUtils.createBlockDiaChi(phieu.getNoiThuongTruTinhId(), phieu.getNoiThuongTruQuanHuyenId(), phieu.getNoiThuongTruPhuongXaId(), phieu.getNoiThuongTruDiaChi());
			}
			trangThai = ts.getXoaAnTich() ? TrangThaiXuLyHoSoXoaAnTichEnum.DA_XOA_AN_TICH.getKey() : TrangThaiXuLyHoSoXoaAnTichEnum.DOI_KY_DUYET.getKey();
		}
		%>
		
		<liferay-ui:search-container-column-text name='ho-va-ten-doi-tuong-tien-su' property="hoVaTenDoiTuongTienSu" orderable="<%=true %>" orderableProperty="hoVaTenDoiTuongTienSu"/>
		<liferay-ui:search-container-column-text name='dia-chi' ><%=diaChi %></liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text cssClass="txt-center" name='ban-an-so'><%=banAnSo %></liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text cssClass="txt-center" name='ma-ho-so-xoa-an-tich' property="maHoSoXoaAnTich"/>
		<liferay-ui:search-container-column-text cssClass="txt-center" name='ngay-lap-ho-so'><%=Validator.isNotNull(lichSuXoaAnTich.getNgayYeuCauXoaAnTich()) ? sdf.format(lichSuXoaAnTich.getNgayYeuCauXoaAnTich()) :""%></liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text name='trang-thai'><liferay-ui:message key="<%=trangThai %>" /></liferay-ui:search-container-column-text>

		<liferay-ui:search-container-column-jsp cssClass="aui-Action" path="/html/tiensu/xoa_an_tich/actions.jsp" align="right" />
	</liferay-ui:search-container-row>
	<liferay-ui:search-iterator searchContainer="<%=yeuCauXoaAnTichSearch %>" />

</liferay-ui:search-container>